from pydantic import BaseModel, EmailStr
from datetime import datetime
from typing import Optional, List
from enum import Enum

# 职位枚举
class PositionEnum(str, Enum):
    medical_student = "医学生"
    resident_doctor = "进修医生"
    deputy_director = "副主任"
    director = "主任"
    dean = "院长"
    other = "其他"

# 用户角色枚举
class UserRole(str, Enum):
    admin = "admin"
    user = "user"

# 用户相关的模式
class UserBase(BaseModel):
    username: str
    email: EmailStr
    phone: str
    hospital: str
    position: PositionEnum

class UserCreate(UserBase):
    password: str
    role: Optional[UserRole] = UserRole.user

class UserLogin(BaseModel):
    username: str
    password: str

class UserResponse(UserBase):
    id: int
    role: UserRole
    is_active: bool
    created_at: datetime
    
    class Config:
        from_attributes = True

# 登录记录相关的模式
class LoginRecordBase(BaseModel):
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None
    status: str = "success"

class LoginRecordCreate(LoginRecordBase):
    user_id: int

class LoginRecordResponse(LoginRecordBase):
    id: int
    user_id: int
    login_time: datetime
    username: Optional[str] = None  # 用于显示用户名
    
    class Config:
        from_attributes = True

# 数据处理记录相关的模式
class DataProcessingRecordBase(BaseModel):
    operation_type: str
    operation_details: Optional[str] = None
    file_name: Optional[str] = None
    duration_ms: Optional[int] = None
    status: str = "success"
    error_message: Optional[str] = None

class DataProcessingRecordCreate(DataProcessingRecordBase):
    user_id: int

class DataProcessingRecordResponse(DataProcessingRecordBase):
    id: int
    user_id: int
    processing_time: datetime
    username: Optional[str] = None  # 用于显示用户名
    
    class Config:
        from_attributes = True

# 管理员查看记录的查询参数
class RecordQueryParams(BaseModel):
    user_id: Optional[int] = None
    username: Optional[str] = None
    start_date: Optional[datetime] = None
    end_date: Optional[datetime] = None
    page: int = 1
    page_size: int = 20

# 分页响应
class PaginatedResponse(BaseModel):
    total: int
    page: int
    page_size: int
    total_pages: int
    items: List[dict]

# Token相关的模式
class Token(BaseModel):
    access_token: str
    token_type: str
    expires_in: int
    user_role: UserRole  # 添加用户角色信息

class TokenData(BaseModel):
    username: Optional[str] = None
    role: Optional[UserRole] = None

# 响应消息
class Message(BaseModel):
    message: str 